//
//  AppDelegate.swift
//  PhaseExcitedMotion
//
//  Created by iOS-Czz on 2023/11/16.
//

import UIKit
import IQKeyboardManagerSwift
import WXNavigationBar

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        let isFirst = UserDefaults.standard.bool(forKey: "isFirst")
        if isFirst == false{
            self.configDefaultData()
        }
        
        IQKeyboardManager.shared.enable = true
        WXNavigationBar.setup()
        let rootViewController = PEMTabBarController()
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.backgroundColor = .white
        window?.rootViewController = rootViewController
        window?.makeKeyAndVisible()
        return true
    }
    
    func configDefaultData(){
        UserDefaults.standard.setValue(15000, forKey: "DefaultSteps")
        UserDefaults.standard.setValue(true, forKey: "isFirst")
        do {
            let path = Bundle.main.path(forResource: "PEMDailyExerciseData", ofType: "json")
            let jsonData = try Data(contentsOf: URL(fileURLWithPath: path!), options: .mappedIfSafe)
            let jsonString = String(data: jsonData, encoding: .utf8)
            let helper = PEMDailyExerciseModel.getUsingLKDBHelper()
            helper.getTableCreated(with: PEMDailyExerciseModel.self)
            let data = jsonString!.data(using: String.Encoding.utf8)
            if let jsonData = data {
                let decoder = JSONDecoder()
                let persons = try! decoder.decode([PEMDailyExerciseModel].self, from: jsonData)
                for index in 0...persons.count-1 {
                    let model = persons[index]
                    model.date = Date().toString(format: "yyyy.MM.dd")
                    model.saveToDB()
                }
            }
        } catch {
            print("Failed to read JSON file: \(error.localizedDescription)")
        }
        
        do {
            let path = Bundle.main.path(forResource: "PEMMotionCongruenceData", ofType: "json")
            let jsonData = try Data(contentsOf: URL(fileURLWithPath: path!), options: .mappedIfSafe)
            let jsonString = String(data: jsonData, encoding: .utf8)
            let helper = PEMMotionCongruenceModel.getUsingLKDBHelper()
            helper.getTableCreated(with: PEMMotionCongruenceModel.self)
            let data = jsonString!.data(using: String.Encoding.utf8)
            if let jsonData = data {
                let decoder = JSONDecoder()
                let persons = try! decoder.decode([PEMMotionCongruenceModel].self, from: jsonData)
                for index in 0...persons.count-1 {
                    let model = persons[index]
                    model.saveToDB()
                }
            }
        } catch {
            print("Failed to read JSON file: \(error.localizedDescription)")
        }
        
        
        do {
            let path = Bundle.main.path(forResource: "PEMChallengeData", ofType: "json")
            let jsonData = try Data(contentsOf: URL(fileURLWithPath: path!), options: .mappedIfSafe)
            let jsonString = String(data: jsonData, encoding: .utf8)
            let helper = PEMChallengeModel.getUsingLKDBHelper()
            helper.getTableCreated(with: PEMChallengeModel.self)
            let data = jsonString!.data(using: String.Encoding.utf8)
            if let jsonData = data {
                let decoder = JSONDecoder()
                let persons = try! decoder.decode([PEMChallengeModel].self, from: jsonData)
                for index in 0...persons.count-1 {
                    let model = persons[index]
                    model.date = Date().toString(format: "yyyy.MM.dd")
                    model.saveToDB()
                }
            }
        } catch {
            print("Failed to read JSON file: \(error.localizedDescription)")
        }
    }
    
}

